package com.chengdu.chun.mapper;

import com.chengdu.chun.po.Classes;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ClassesMapper {

    @Select("select * from classes where cname=#{cname}")
    public Classes selectClassesByName(String cname);


    /**
     * 添加新的班级
     *
     * @param classes
     * @return
     */
    @Insert("insert into classes values (null,#{cname},#{cgrade},#{uid},curdate(),now(),1)")
    public int insertClasses(Classes classes);

    /**
     * 根据班级序号获取班级详情
     *
     * @param classesid
     * @return
     */
    @Select("select * from classes where cid=#{cid}")
    public Classes getClassesBycid(int classesid);

    /**
     * 根据老师获取对应管理的在读班级数量
     *
     * @param teacherid
     * @return
     */
    @Select("select count(*) from classes where uid=#{teacherid} and cstatus = 1")
    public Integer selectCountByTeacher(int teacherid);

    /**
     * 根据老师获取对应管理的在读班级的集合
     *
     * @param teacherid
     * @return
     */
    @Select("        select c.*, count(s.sid) as studentsnum " +
            "           from classes c left join student s on c.cid = s.cid " +
            "           where c.uid = #{teacherid} " +
            "           group by c.cid " +
            "           order by cstatus desc, cid desc;")
    public List<Classes> getClassesByTeacher(int teacherid);

    @Select("select count(*) from classes where uid = #{teacherid} ")
    public long getClassesCountByTeacher(int teacherid);

    /**
     * 根据页码进行查询进行操作
     *
     * @return
     */
    @Select("        select c.*, count(s.sid) as studentsnum " +
            "           from classes c left join student s on c.cid = s.cid " +
            "           where c.uid = #{teacherid} " +
            "           group by c.cid " +
            "           order by cstatus desc, cid desc " +
            " limit #{rownum}, #{number}")
    public List<Classes> getClassesByTeacherAndPage(@Param("teacherid") int teacherid,
                                                    @Param("rownum") long rownum, @Param("number") int number);

    public List<Classes> getClassesByTeacherAndName(@Param("cname") String classesname, @Param("teacherid") int teacherid);

    int deleteByPrimaryKey(Integer cid);

    int insert(Classes record);

    int insertSelective(Classes record);

    Classes selectByPrimaryKey(Integer cid);

    int updateByPrimaryKeySelective(Classes record);

    int updateByPrimaryKey(Classes record);
}